<template>
	<view class="wrap">
		<view class="top">
			<view class="item">
				<view class="left">收货人</view>
				<u-input v-model="address.name" type="text" fontSize="17" clearable border="none"
					placeholder-class="line" placeholder="请填写收货人姓名" />
				</u-input>
			</view>
			<view class="item">
				<view class="left">手机号码</view>
				<u-input type="number" v-model="address.phone" maxlength="11" fontSize="17" clearable border="none"
					placeholder-class="line" placeholder="请填写收货人手机号" />
				</u-input>
			</view>
			<view class="item">
				<view class="left">所在地区</view>
				<view class="right" @click="lotusAddressData.visible = true">
					<view class="address" :style="{color:region?'black':'#c0c4cc'}">{{region ? region : '点击选择省市县名称值' }}
					</view>
					<view>
						<u-icon name="arrow-right" :size="22" color="#999999"></u-icon>
					</view>
				</view>
			</view>
			<view class="item address">
				<view class="left">详细地址</view>
				<textarea type="text" v-model="address.address" fontSize="17" placeholder-class="line"
					placeholder="街道、楼牌等" />
			</view>
		</view>
		<view class="bottom">
			<view class="default">
				<view class="left">
					<view class="set">设置默认地址</view>
					<view class="tips">提醒：每次下单会默认推荐该地址</view>
				</view>
				<view class="right">
					<switch :checked="address.is_default == 1 ?true:false" color="red" @change="setDefault" />
				</view>
			</view>
		</view>
		<view class="addSite" :style="[inputStyle]" @click="modify">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>提交
			</view>
		</view>
		<lotusAddress v-on:choseVal="choseValue" :lotusAddressData="lotusAddressData"></lotusAddress>
	</view>
</template>

<script>
	let access_token, _options;
	export default {
		data() {
			return {
				lotusAddressData: {
					visible: false, //此变量绝定了选择器是否显示
					provinceName: '', //省
					cityName: '', //市
					townName: '', //县
				},
				region: '', //最后返回的省市县的组合
				address: {
					name: '',
					phone: '',
					address: '',
					is_default: 0 //是否默认地址
				}
			}
		},
		computed: {
			inputStyle() {
				let style = {};
				if (uni.$u.test.mobile(this.address.phone) && this.address.name && this.address
					.address && this.region) {
					style.color = "#fff";
					style.backgroundColor = uni.$u.color['warning'];
				}
				return style;
			}
		},
		async onLoad(options) {
			if (!uni.$u.utils.isLogin()) {
				return;
			}
			_options = options;
			access_token = uni.getStorageSync('access_token');
			let res = await uni.$u.http.get(`/api/address/${options.id}`, {
				header: {
					Authorization: 'bearer ' + access_token
				}
			});
			console.log(res);
			this.address = res;
			this.lotusAddressData.provinceName = res.province;
			this.lotusAddressData.cityName = res.city;
			this.lotusAddressData.townName = res.county;
			this.region = res.province + '-' + res.city + (res.county ? '-' + res.county : '');
		},
		methods: {
			//回传已选的省市区的值
			choseValue(res) {
				//res数据源包括已选省市区与省市区code
				console.log(res);
				this.lotusAddressData.visible = res.visible; //visible为显示与关闭组件标识true显示false隐藏
				//res.isChose = 1省市区已选 res.isChose = 0;未选
				if (res.isChose) {
					this.lotusAddressData.provinceName = res.province; //省
					this.lotusAddressData.cityName = res.city; //市
					this.lotusAddressData.townName = res.town; //区
					this.region = res.province + '-' + res.city + (res.county ? '-' + res.county : '');
				}
			},
			setDefault(e) {
				this.address.is_default = e.target.value ? 1 : 0;
			},
			async modify() {
				if (!(uni.$u.test.mobile(this.address.phone) && this.address.name && this.address
						.address && this.region)) {
					return;
				}
				access_token = uni.getStorageSync('access_token');
				const params = {
					province: this.lotusAddressData.provinceName,
					city: this.lotusAddressData.cityName,
					county: this.lotusAddressData.townName,
					name: this.address.name,
					phone: this.address.phone,
					address: this.address.address,
					is_default: this.address.is_default
				};
				await uni.$u.http.put(`/api/address/${_options.id}`, params, {
					header: {
						Authorization: 'bearer ' + access_token
					}
				});
				uni.$u.toast('地址修改成功');
				setTimeout(() => {
					uni.$u.route({
						type: 'redirect',
						url: '/pages/cart/address'
					}); //返回，重新刷新地址内容
				}, 1000)
			}
		}
	}
</script>

<style lang="scss" scoped>
	/deep/ .line {
		color: $u-light-color;
		font-size: 17px;
	}

	/deep/ .u-line {
		border: none !important;
	}

	/deep/ .u-cell__title-text {
		font-size: 17px;
		color: rgb(192, 196, 204);
	}

	/deep/ .u-cell__body {
		padding: 10px 0;
	}

	.wrap {
		background-color: #f2f2f2;

		.top {
			background-color: #ffffff;
			border-top: solid 2rpx $u-border-color;
			padding: 22rpx;

			.item {
				display: flex;
				font-size: 32rpx;
				line-height: 100rpx;
				align-items: center;
				border-bottom: solid 2rpx $u-border-color;

				.left {
					width: 180rpx;
				}

				.right {
					display: flex;
					justify-content: space-between;
					align-items: center;
					width: 100%;
					flex: 1;

					.address {
						line-height: 130%;
					}
				}
			}

			.address {
				padding: 20rpx 0;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}
			}

			.site-clipboard {
				padding-right: 40rpx;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}

				.clipboard {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					color: $u-tips-color;
					height: 80rpx;

					.icon {
						margin-top: 6rpx;
						margin-left: 10rpx;
					}
				}
			}
		}

		.bottom {
			padding: 10rpx 40rpx 10rpx 40rpx;
			background-color: #ffffff;
			font-size: 28rpx;

			.default {
				display: flex;
				justify-content: space-between;
				border-bottom: solid 2rpx $u-border-color;
				line-height: 64rpx;

				.tips {
					font-size: 24rpx;
					color: blue;
				}

				.right {}
			}
		}

		.addSite {
			display: flex;
			justify-content: space-around;
			width: 600rpx;
			line-height: 100rpx;
			position: fixed;
			bottom: 30rpx;
			left: 80rpx;
			background-color: #aaa;
			border-radius: 60rpx;
			font-size: 30rpx;

			.add {
				display: flex;
				align-items: center;
				color: #ffffff;

				.icon {
					margin-right: 10rpx;
				}
			}
		}
	}
</style>
